from selenium import webdriver
from time import sleep
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .login import login_test
from .verification import main
from selenium.webdriver.chrome.options import Options


def open_browser(browser_name, url):
    driver = None
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--window-size=1920,1080')
    if browser_name == "chrome":
        driver = webdriver.Chrome(chrome_options=chrome_options)
    if browser_name == "ff":
        driver = webdriver.Firefox()
    if browser_name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, browser_name, url, username, password, new_password):
        self.driver = open_browser(browser_name, url)
        self.username = username
        self.password = password
        self.new_password = new_password

    def webui1(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 操作日志
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[5]/span').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 筛选条件-用户名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').send_keys(dicts["username2"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div/button[1]').click()
            sleep(2)
            error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
            print(error_text)
            if error_text == "":
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                print(error_text)
            # 重置
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div/button[2]').click()
            sleep(2)
            returns_list.append(error_text)
            driver.refresh()
            sleep(3)
            continue
        print(returns_list)
        return returns_list

    def webui2(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 操作日志
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[5]/span').click()
        sleep(1)
        # 操作时间
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[2]/div/div/div/i[1]').click()
        sleep(2)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[2]/table/thead/tr/th[1]/div/label/span').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[2]/table/thead/tr/th[1]/div/label/span').click()
        sleep(2)
        # 页面下滑到底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        sleep(2)
        # 下一页点4次
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/button[2]').click()
        sleep(2)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/button[2]').click()
        sleep(2)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/button[2]').click()
        sleep(2)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/button[2]').click()
        sleep(2)
        return "执行成功"

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()
